/**
 * Copyright (c) 2017, Alex. All rights reserved.
 */
package com.oschina.bottle.utils.validator;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式校验工具
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2017/9/27
 * @since 1.0
 */
public class RegExpValidator {

    //电话号码正则表达式
    public final static String MOBILE_REG = "^[1][3,4,5,6, 7,8,9][0-9]{9}$";

    //固话，携带区号
    public final static String CODE_PHONE_REG = "^[0][1-9]{2,3}-[0-9]{5,10}$";

    //固话，没有区号
    public final static String PHONE_REG = "^[1-9]{1}[0-9]{5,8}$";

    //数字验证, 均为数字
    public final static String NUM_REG = "^[0-9]+$";

    //中文
    public final static String CHINESE_REG = "^[\\u4e00-\\u9fa5]{0,}$";

    //字母和数字
    public final static String LETTER_NUM_REG = "^[A-Za-z0-9]+$";

    //字母
    public final static String LETTER_REG = "^[A-Za-z]+$";

    //小写字母
    public final static String LOW_LETTER_REG = "^[a-z]+$";

    //大写字母
    public final static String UP_LETTER_REG = "^[A-Z]+$";

    //数字、英文字母、下划线组成的字符串
    public final static String UNDERLINE_LETTER_NUM_REG = "^\\w+$";

    //中文、字母、数字组成的字符串
    public final static String CHINESE_LETTER_NUM_REG = "[\\u4E00-\\u9FA5A-Za-z0-9]+$";

    /**
     * 正则校验
     *
     * @param regExp 正则表达式
     * @param validateStr 待校验的字符串
     */
    public static boolean validate(String regExp, String validateStr) {
        Objects.requireNonNull(regExp, "正则表达式不能为空");

        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(validateStr);
        return m.matches();
    }
}
